package cn.devices.controller;


import cn.devices.entity.TbAdm;
import cn.devices.model.JsonMessage;
import cn.devices.security.AuthUserDetail;
import cn.devices.service.TbAdmService;
import cn.hutool.crypto.SecureUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 管理员表 前端控制器
 * </p>
 *
 * @author 秋水浮萍
 * @since 2021-02-07
 */
@RestController
@RequestMapping("/tb-adm")
public class TbAdmController {


    @Autowired
    private TbAdmService tbAdmService;

    @GetMapping("info")
    @ResponseBody
    public JsonMessage userinfo(Authentication auth) {
        AuthUserDetail detail = (AuthUserDetail) auth.getDetails();
        //AuthUserDetail user=JwtUtil.parseJWT(token);
        // SysUser user =userService.getByLoginName(auth.getName());
        JsonMessage message = new JsonMessage();
        // message.setData(user);
        Map<String, Object> data = new HashMap<>();
        data.put("name", detail.getUsername());
        data.put("tbAdm", tbAdmService.getByAdmId(Integer.parseInt(detail.getUserId())));
        data.put("avatar", "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        message.setData(data);
        return message;
    }

    //添加管理员
    @PostMapping("admAdd")
    public JsonMessage admAdd(@RequestBody TbAdm tbAdm) {
        JsonMessage message = new JsonMessage();
        if (tbAdmService.confirmLoginName(tbAdm.getLoginName(), tbAdm.getAdminTel()) == 0) {
            tbAdmService.tbAdmAdd(tbAdm);
        } else {
            message.setData(0);
        }
        return message;
    }

    //查询所有管理员 根据状态0,1 id排序
    @PostMapping("tbAdmList")
    public JsonMessage tbAdmList(@RequestParam(defaultValue = "1") int pageIndex,
                                 @RequestParam(defaultValue = "10") int pageSize) {
        JsonMessage message = new JsonMessage();
        PageInfo<TbAdm> pageInfo = tbAdmService.tbAdmList(pageIndex, pageSize);
        message.setData(pageInfo);
        return message;
    }

    //根据id查管理员信息
    @GetMapping("getByAdmId")
    public JsonMessage getByAdmId(Integer admId) {
        JsonMessage message = new JsonMessage();
        message.setData(tbAdmService.getByAdmId(admId));
        return message;
    }

    //修改管理员手机号
    @PostMapping("tbAdmUpdate")
    public JsonMessage tbAdmUpdate(@RequestBody TbAdm tbAdm) {
        JsonMessage message = new JsonMessage();
        if (tbAdmService.tbAdmTelCount(tbAdm) > 0) {
            message.setData("手机号已存在");
            return message;
        }
        tbAdmService.tbAdmUpdate(tbAdm);
        message.setData("修改成功");
        return message;
    }

    /**
     * 修改管理员密码
     *
     * @param admId        id
     * @param loginPassOld 旧密码
     * @param loginPassNew 新密码
     * @return
     */
    @PostMapping("tbAdmPassUpdate")
    public JsonMessage tbAdmPassUpdate(Integer admId, String loginPassOld, String loginPassNew) {
        JsonMessage message = new JsonMessage();
        //密码加密
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String loginPassCorrect = tbAdmService.loginPassCorrect(admId);
        boolean matches = passwordEncoder.matches(loginPassOld,loginPassCorrect);
        if (matches ) {
            tbAdmService.loginPassUpdate(admId, loginPassNew);
            message.setData("修改成功");
        } else {
            message.setData("原密码错误");
            return message;
        }
        return message;
    }

    //修改登陆时间
    @GetMapping("lastLogin")
    public JsonMessage lastLogin(Authentication authentication) {
        JsonMessage message = new JsonMessage();
        AuthUserDetail detail = (AuthUserDetail) authentication.getDetails();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String lastLogin = sdf.format(new Date());
        tbAdmService.lastLogin(lastLogin, detail.getUserId());
        message.setData("修改登陆时间");
        return message;
    }


    //假删除
    @GetMapping("tbAdminDeleteFalse")
    public JsonMessage tbAdminDeleteFalse(Integer adminId) {
        JsonMessage message = new JsonMessage();
        tbAdmService.tbAdmDeleteFalse(adminId);
        message.setData("删除成功");
        return message;
    }

    //真删除
    @GetMapping("tbAdminDeleteTrue")
    public JsonMessage tbAdminDeleteTrue(Integer adminId) {
        JsonMessage message = new JsonMessage();
        tbAdmService.tbAdmDeleteTrue(adminId);
        message.setData("删除成功");
        return message;
    }

    //退出登录
    @PostMapping("logout")
    public JsonMessage logout(@RequestHeader("X-Token") String token) {
        JsonMessage message = new JsonMessage();
        return message;
    }


}
